package com.cyl.manager.aws.service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyl.manager.aws.domain.entity.ProfitDetailsStatistics;
import com.cyl.manager.aws.domain.entity.ShopStatistics;
import com.cyl.manager.aws.mapper.ProfitDetailsStatisticsMapper;
import com.cyl.manager.aws.pojo.query.ProfitDetailsStatisticsQuery;
import com.cyl.manager.aws.pojo.vo.ProfitDetailsStatisticsSumVO;
import com.cyl.manager.aws.pojo.vo.ProfitDetailsStatisticsVO;
import com.cyl.manager.aws.pojo.vo.ProfitStatisticsSqlVO;
import com.cyl.manager.pms.domain.entity.Brand;
import com.cyl.manager.pms.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;

import cn.hutool.core.date.DateUtil;

/**
 * 毛利润统计明细Service业务层处理
 *
 *
 * @author hxw
 */
@Service
public class ProfitDetailsStatisticsService {
    @Autowired
    private ProfitDetailsStatisticsMapper profitDetailsStatisticsMapper;

    /**
     * 查询毛利润统计明细
     *
     * @param id 毛利润统计明细主键
     * @return 毛利润统计明细
     */
    public ProfitDetailsStatistics selectById(String id) {
        return profitDetailsStatisticsMapper.selectById(id);
    }
    
    /**
     * 	获取最新更新时间
     * @param query
     * @param page
     * @return
     */
    public String selectMaxCreateTime() {
    	LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(5L);
        QueryWrapper<ProfitDetailsStatistics> queryWrapper = new QueryWrapper<>();
        queryWrapper.select( "max(create_time) create_time");
        ProfitDetailsStatistics profitDetailsStatistics = profitDetailsStatisticsMapper.selectOne(queryWrapper);
        if(profitDetailsStatistics!=null) {
        	localDateTime =  profitDetailsStatistics.getCreateTime();
        }
        return DateUtil.formatLocalDateTime(localDateTime);

    }
    

    /**
     * 查询毛利润统计明细列表
     *
     * @param query 查询条件
     * @param page 分页条件
     * @return 毛利润统计明细
     */
    public List<ProfitDetailsStatistics> selectList(ProfitDetailsStatisticsQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<ProfitDetailsStatistics> qw = new QueryWrapper<>();
        String selectGroupBy = query.getSelectGroupBy();
       
        if(selectGroupBy!=null && !"".equals(selectGroupBy)) {
        	String strGroup = ",dept_zbj_id,dept_tz_id,dept_dp_id,brand_id,tjdata,";
        	String[] groupBys =  selectGroupBy.split(",");
        	boolean bool = false;
        	for (String group : groupBys) {
        		if(strGroup.contains((","+group+","))) {
        			bool = true;
        		}
			}
        	if(bool == false) {
                throw new RuntimeException("无效的统计字段");
        	}
            qw.select(selectGroupBy + "	,\r\n" + 
     		   		"			sum(IFNULL(pay_quantity, 0)) pay_quantity,\r\n" + 
     		   		"			sum(IFNULL(pay_amount, 0)) pay_amount,\r\n" + 
     		   		"			sum(IFNULL(pay_fee_amount, 0)) pay_fee_amount,\r\n" + 
     		   		"			sum(IFNULL(pay_gys_amount, 0)) pay_gys_amount,\r\n" + 
     		   		"			sum(IFNULL(pay_dp_amount, 0)) pay_dp_amount,\r\n" + 
     		   		"			sum(IFNULL(pay_conut, 0)) pay_conut,\r\n" + 
     		   		"			sum(IFNULL(ret_quantity, 0)) ret_quantity,\r\n" + 
     		   		"			sum(IFNULL(ret_amount, 0)) ret_amount,\r\n" + 
     		   		"			sum(IFNULL(ret_fee_amount, 0)) ret_fee_amount,\r\n" + 
     		   		"			sum(IFNULL(ret_gys_amount, 0)) ret_gys_amount,\r\n" + 
     		   		"			sum(IFNULL(ret_dp_amount, 0)) ret_dp_amount,\r\n" + 
     		   		"			sum(IFNULL(ret_conut, 0)) ret_conut,\r\n" + 
     		   		"			sum(IFNULL(pay_sys_amount, 0)) pay_sys_amount,\r\n" + 
     		   		"			sum(IFNULL(ret_sys_amount, 0)) ret_sys_amount");
            qw.groupBy(selectGroupBy);
        }else {
        	 qw.select("*"); 
        }
        
        
        Integer[] tjdataRange = query.getTjdataRange();
        if (tjdataRange != null && tjdataRange.length == 2) {
            Integer start = tjdataRange[0];
            if (start != null) {
                qw.ge("tjdata", start);
            }
            Integer end = tjdataRange[1];
            if (end != null) {
                qw.le("tjdata", end);
            }
        }
        Integer tjyear = query.getTjyear();
        if (tjyear != null) {
            qw.eq("tjyear", tjyear);
        }
        Integer tjmonth = query.getTjmonth();
        if (tjmonth != null) {
            qw.eq("tjmonth", tjmonth);
        }
        Integer tjweek = query.getTjweek();
        if (tjweek != null) {
            qw.eq("tjweek", tjweek);
        }
        Integer tjday = query.getTjday();
        if (tjday != null) {
            qw.eq("tjday", tjday);
        }
        Long deptZbjId = query.getDeptZbjId();
        if (deptZbjId != null) {
            qw.eq("dept_zbj_id", deptZbjId);
        }
        Long deptTzId = query.getDeptTzId();
        if (deptTzId != null) {
            qw.eq("dept_tz_id", deptTzId);
        }
        Long deptDpId = query.getDeptDpId();
        if (deptDpId != null) {
            qw.eq("dept_dp_id", deptDpId);
        }
        Long brandId = query.getBrandId();
        if (brandId != null) {
            qw.eq("brand_id", brandId);
        }
        Integer payQuantity = query.getPayQuantity();
        if (payQuantity != null) {
            qw.eq("pay_quantity", payQuantity);
        }
        BigDecimal payAmount = query.getPayAmount();
        if (payAmount != null) {
            qw.eq("pay_amount", payAmount);
        }
        BigDecimal payFeeAmount = query.getPayFeeAmount();
        if (payFeeAmount != null) {
            qw.eq("pay_fee_amount", payFeeAmount);
        }
        BigDecimal payGysAmount = query.getPayGysAmount();
        if (payGysAmount != null) {
            qw.eq("pay_gys_amount", payGysAmount);
        }
        BigDecimal payDpAmount = query.getPayDpAmount();
        if (payDpAmount != null) {
            qw.eq("pay_dp_amount", payDpAmount);
        }
        Integer payConut = query.getPayConut();
        if (payConut != null) {
            qw.eq("pay_conut", payConut);
        }
        Integer retQuantity = query.getRetQuantity();
        if (retQuantity != null) {
            qw.eq("ret_quantity", retQuantity);
        }
        BigDecimal retAmount = query.getRetAmount();
        if (retAmount != null) {
            qw.eq("ret_amount", retAmount);
        }
        BigDecimal retFeeAmount = query.getRetFeeAmount();
        if (retFeeAmount != null) {
            qw.eq("ret_fee_amount", retFeeAmount);
        }
        BigDecimal retGysAmount = query.getRetGysAmount();
        if (retGysAmount != null) {
            qw.eq("ret_gys_amount", retGysAmount);
        }
        BigDecimal retDpAmount = query.getRetDpAmount();
        if (retDpAmount != null) {
            qw.eq("ret_dp_amount", retDpAmount);
        }
        Integer retConut = query.getRetConut();
        if (retConut != null) {
            qw.eq("ret_conut", retConut);
        }
        return profitDetailsStatisticsMapper.selectList(qw);
    }


    
    /**
     * 批量删除店铺数据统计
     *
     * @param ids 需要删除的店铺数据统计主键
     * @return 结果
     */
    @Transactional
    public  int insertProfitDay(LocalDateTime strTime,LocalDateTime endTime) {
       	int tjdata = Integer.parseInt(DateUtil.format(strTime, "yyyyMMdd"));
 		int	tjyear = Integer.parseInt(DateUtil.format(strTime, "yyyy"));
 		int	tjmonth = Integer.parseInt(DateUtil.format(strTime, "MM"));
 		int	tjweek = Integer.parseInt(DateUtil.format(strTime, "w"));
 		int	tjday = Integer.parseInt(DateUtil.format(strTime, "dd"));
 		ProfitStatisticsSqlVO profitStatisticsSqlVO = new ProfitStatisticsSqlVO();
 		profitStatisticsSqlVO.setCreateTime(LocalDateTime.now());
 		profitStatisticsSqlVO.setStrTime(strTime);
 		profitStatisticsSqlVO.setEndTime(endTime);
 		profitStatisticsSqlVO.setTjdata(tjdata);
 		profitStatisticsSqlVO.setTjyear(tjyear);
 		profitStatisticsSqlVO.setTjmonth(tjmonth);
 		profitStatisticsSqlVO.setTjweek(tjweek); 
 		profitStatisticsSqlVO.setTjday(tjday);
    	QueryWrapper<ProfitDetailsStatistics> wrapper = new QueryWrapper<ProfitDetailsStatistics>();
    	wrapper.eq("tjdata", profitStatisticsSqlVO.getTjdata());
    	profitDetailsStatisticsMapper.delete(wrapper);
        return profitDetailsStatisticsMapper.insertProfitDay(profitStatisticsSqlVO);
    }
///////////////////////////////////////////
    
    
    @Autowired
    private ISysDeptService deptService;

    
    @Autowired
    private BrandService brandService;

    public List<Object> dos2vos(List<ProfitDetailsStatistics> list,String type){
    	List<Object> retList =  new ArrayList<Object>();
        if(list!=null) {
        	for (ProfitDetailsStatistics pds : list) {
        		Long deptId = pds.getDeptDpId();
        		Long brandId = pds.getBrandId();

        		String deptDpmc = "";
        		String brandmc = "";
        		if(deptId!=null) {
        			SysDept sysDept = deptService.getCacheSysDept(deptId);
        			if(sysDept!=null) {
        				deptDpmc = sysDept.getDeptName();
        			}
        		}
        		if(brandId!=null) {
        			Brand brand = brandService.getCacheBrand(brandId);
        			if(brand!=null) {
        				brandmc = brand.getName();
        			}
        		}
        		ProfitDetailsStatisticsVO vo = new ProfitDetailsStatisticsVO(pds, brandmc, deptDpmc);
				if(type!=null && type.equals("sum")) {
					ProfitDetailsStatisticsSumVO sumVo = new ProfitDetailsStatisticsSumVO(vo);
					retList.add(sumVo);
				}else {
					retList.add(vo);
				}
			}
        }
		return retList;
    }

    
    public List<ProfitDetailsStatisticsVO> dos2vosVo(List<ProfitDetailsStatistics> list){
    	List<ProfitDetailsStatisticsVO> retList =  new ArrayList<ProfitDetailsStatisticsVO>();
        if(list!=null) {
        	for (ProfitDetailsStatistics pds : list) {
        		Long deptId = pds.getDeptDpId();
        		Long brandId = pds.getBrandId();

        		String deptDpmc = "";
        		String brandmc = "";
        		if(deptId!=null) {
        			SysDept sysDept = deptService.getCacheSysDept(deptId);
        			if(sysDept!=null) {
        				deptDpmc = sysDept.getDeptName();
        			}
        		}
        		if(brandId!=null) {
        			Brand brand = brandService.getCacheBrand(brandId);
        			if(brand!=null) {
        				brandmc = brand.getName();
        			}
        		}
        		ProfitDetailsStatisticsVO vo = new ProfitDetailsStatisticsVO(pds, brandmc, deptDpmc);
        		retList.add(vo);
			}
        }
		return retList;
    }
    
    public List<ProfitDetailsStatisticsSumVO> dos2vosVoSum(List<ProfitDetailsStatistics> list){
    	List<ProfitDetailsStatisticsSumVO> retList =  new ArrayList<ProfitDetailsStatisticsSumVO>();
        if(list!=null) {
        	for (ProfitDetailsStatistics pds : list) {
        		Long deptId = pds.getDeptDpId();
        		Long brandId = pds.getBrandId();

        		String deptDpmc = "";
        		String brandmc = "";
        		if(deptId!=null) {
        			SysDept sysDept = deptService.getCacheSysDept(deptId);
        			if(sysDept!=null) {
        				deptDpmc = sysDept.getDeptName();
        			}
        		}
        		if(brandId!=null) {
        			Brand brand = brandService.getCacheBrand(brandId);
        			if(brand!=null) {
        				brandmc = brand.getName();
        			}
        		}
	        		ProfitDetailsStatisticsVO vo = new ProfitDetailsStatisticsVO(pds, brandmc, deptDpmc);
	        		ProfitDetailsStatisticsSumVO sumVo = new ProfitDetailsStatisticsSumVO(vo);
					retList.add(sumVo);
				}
        }
		return retList;
    }
    
   
}
